﻿using System;
using System.Data;
using System.Web.UI.WebControls;

public partial class MRefundsPrint : System.Web.UI.Page
{
    private readonly string mPageName = "Print";
    public DateTime fTranzactionDate = new DateTime();
    public decimal fTranzactionAmount = 0;
    #region principal
    private decimal principalOriginalSummary = 0;
    private decimal principalPaidSummary = 0;
    private decimal principalWaivedSummary = 0;
    private decimal principalWrittenOffSummary = 0;
    private decimal principalOutstandingSummary = 0;
    private decimal principalOverDueSummary = 0;
    #endregion
    #region interest
    private decimal interestOriginalSummary = 0;
    private decimal interestPaidSummary = 0;
    private decimal interestWaivedSummary = 0;
    private decimal interestWrittenOffSummary = 0;
    private decimal interestOutstandingSummary = 0;
    private decimal interestOverDueSummary = 0;
    #endregion
    #region Fees
    private decimal feesOriginalSummary = 0;
    private decimal feesPaidSummary = 0;
    private decimal feesWaivedSummary = 0;
    private decimal feesWrittenOffSummary = 0;
    private decimal feesOutstandingSummary = 0;
    private decimal feesOverDueSummary = 0;
    #endregion
    #region Penalties
    private decimal penaltiesOriginalSummary = 0;
    private decimal penaltiesPaidSummary = 0;
    private decimal penaltiesWaivedSummary = 0;
    private decimal penaltiesWrittenOffSummary = 0;
    private decimal penaltiesOutstandingSummary = 0;
    private decimal penaltiesOverDueSummary = 0;
    #endregion
    #region Total
    private decimal totalOriginalSummary = 0;
    private decimal totalPaidSummary = 0;
    private decimal totalWaivedSummary = 0;
    private decimal totalWrittenOffSummary = 0;
    private decimal totalOutstandingSummary = 0;
    private decimal totalOverDueSummary = 0;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                int loanID = 0;
                int.TryParse(Request["loanID"], out loanID);

                FillStartUP(loanID);

                FillPreviewForm(loanID);
                FillRepaymentSchedule(loanID);

                FillTransaction(loanID);
            }
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }

    private void FillTransaction(int loanID)
    {
        try
        {
            tranzactionGridView.EditIndex = -1;
            Credits.DataObjects.LoanObject loanInfo = Utils.ModuleCredits().GetLoanObjectByID(loanID);
            if (loanInfo != null)
            {
                string balanceValue = loanInfo.ContractAmount.ToString();
                decimal.TryParse(balanceValue, out fTranzactionAmount);

                fTranzactionDate = loanInfo.ContractDate;
            }

            DataTable tranzactionDT = Utils.ModuleCredits().GetTranzactionsLoanOrderDT(loanID);

            decimal currencyChange = loanInfo.CurrencyChangeRate;
            if (currencyChange > 0)
            {
                for (int index = 0; index < tranzactionDT.Rows.Count; index++)
                {
                    DateTime payDate = (DateTime)tranzactionDT.Rows[index]["TranzactionDate"];
                    if (payDate > DateTime.Now.Date)
                    {
                        payDate = DateTime.Now.Date;
                    }

                    decimal realChangeRate = Utils.ModuleCredits().GetCurrencyRateByDate(payDate, loanInfo.CurrencyID);
                    if (realChangeRate > 0)
                    {
                        tranzactionDT.Rows[index]["Rate"] = realChangeRate;
                    }
                }
            }

            tranzactionDT.AcceptChanges();
            tranzactionGridView.DataSource = tranzactionDT;
            tranzactionGridView.DataBind();

        }
        catch (Exception ex)
        {
            //todo
        }
    }

    private void FillRepaymentSchedule(int loanID)
    {
        if (loanID != 0)
        {
            DataTable ordersDT = Utils.ModuleCredits().GetRefundsLoanOrderDT1(loanID);
            refundGridView.DataSource = ordersDT;
            refundGridView.DataBind();
            if (ordersDT != null && ordersDT.Rows.Count > 0)
            {
                int footerID = ordersDT.Rows.Count - 1;
                decimal.TryParse(ordersDT.Rows[footerID]["principalDueID"].ToString(), out principalOriginalSummary);
                decimal.TryParse(ordersDT.Rows[footerID]["mainPays"].ToString(), out principalPaidSummary);
                principalOutstandingSummary = principalOriginalSummary - principalPaidSummary;

                decimal.TryParse(ordersDT.Rows[footerID]["interestID"].ToString(), out interestOriginalSummary);
                decimal.TryParse(ordersDT.Rows[footerID]["profitPays"].ToString(), out interestPaidSummary);
                interestOutstandingSummary = interestOriginalSummary - interestPaidSummary;

                //decimal.TryParse(ordersDT.Rows[footerID][""].ToString(), out feesOriginalSummary);
                decimal.TryParse(ordersDT.Rows[footerID]["feesID"].ToString(), out feesPaidSummary);
                feesOutstandingSummary = feesOriginalSummary - feesPaidSummary;

                decimal.TryParse(ordersDT.Rows[footerID]["penaltiesID"].ToString(), out penaltiesPaidSummary);
                penaltiesOutstandingSummary = penaltiesPaidSummary;
            }
           
        }
    }

    private void fSetLabelText(Label resultLabel, decimal decimalValue)
    {
        resultLabel.Text = string.Format("{0:0.00}", decimalValue);
    }
    
    protected void FillPreviewForm(int loanID)
    {
        try
        {
            if (loanID != 0)
            {
                Credits.DataObjects.LoanObject loanInfo = Utils.ModuleCredits().GetLoanObjectByID(loanID);
                if (loanInfo != null)
                {
                    clientNameLabelID.Text = loanInfo.ClientFirstLastName;
                    loanLabelID.Text = loanInfo.ContractCode.ToString();

                    decimal sumLoan = 0;
                    decimal percentLoan = 0;
                    decimal.TryParse(loanInfo.ContractAmountInLei.ToString(), out sumLoan);
                    decimal.TryParse(loanInfo.ContrcatPercent.ToString(), out percentLoan);
                    percentLoan = percentLoan / 100;
                    //interestLoanLabel.Text = (sumLoan * percentLoan).ToString(specifier);

                    //totalLoanLabel.Text = (sumLoan + (sumLoan * percentLoan)).ToString(specifier);

                    if (loanInfo.CurrencyID != 0 && loanInfo.CurrencyID != (int)Constants.Constants.CurrencyList.MDL)
                    {
                        decimal initialChangeRate = loanInfo.CurrencyChangeRate;
                        decimal realChangeRate = Utils.ModuleCredits().GetCurrencyRateByDate(DateTime.Now.Date, loanInfo.CurrencyID);

                        string currencyChange = loanInfo.CurrencyChangeRate.ToString();
                        if (string.IsNullOrEmpty(currencyChange))
                        {
                            currencyChange = "1";
                        }

                        if (initialChangeRate < realChangeRate)
                        {
                            Utils.ModuleCredits().ApplyNewChangeRateForloanOrders(loanID, realChangeRate, DateTime.Now.Date);
                        }
                    }
                    else
                    {
                        string currencyChange = loanInfo.CurrencyChangeRate.ToString();
                        if (string.IsNullOrEmpty(currencyChange))
                        {
                            currencyChange = "1";
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }

    #region initierea variabilelor
    decimal sumRateAmountForFooter = 0;
    decimal sumRatePaysForFooter = 0;
    decimal sumRateDiffForFooter = 0;

    decimal sumPercentAmountForFooter = 0;
    decimal sumPercentPaysForFooter = 0;
    decimal sumPercentDiffForFooter = 0;

    decimal sumPenaltyForFooter = 0;
    decimal sumPenaltyPaysForFooter = 0;
    decimal sumPenaltyDiffForFooter = 0;

    decimal sumCorrentionAmountForFooter = 0;
    decimal sumCorrectionPaysForFooter = 0;
    decimal sumCorrectionDiffForFooter = 0;

    decimal sumCommissionForFooter = 0;
    decimal sumCommissionPaysForFooter = 0;
    decimal sumCommissionDiffForFooter = 0;

    decimal sumTotalAmountForFooter = 0;
    decimal sumTotalPaysForFooter = 0;
    decimal sumTotalDiffForFooter = 0;

    #endregion

    DateTime curretDate = DateTime.Now;

    protected void FillStartUP(int loanID)
    {        
        Credits.DataObjects.LoanObject loanInfo = Utils.ModuleCredits().GetLoanObjectByID(loanID);
        if (loanInfo != null)
        {
            currencyLabel.Text = loanInfo.CurrencyChangeRate.ToString(); //sourceDT.Rows[0]["Currency"].ToString();
        }
    }
}